#include <vector>

template<typename T>
class CircleVector : public std::vector<T> {
 private:
    int first_ = 0, end_ = -1;
    int len = -1;

 public:
    explicit CircleVector(int max_num)
    {
        len = max_num;
        this->reserve(len);
    }
    void push_back(const T &val)
    {
        if (std::vector<T>::size() == len)
        {
            end_ = (end_ + 1) % len;
            (*this)[end_] = val;
            first_ = (first_ + 1) % len;
        }
        else
        {
            std::vector<T>::emplace_back(std::move(val));
            ++end_;
        }
    }
    typename std::vector<T>::iterator front()
    { return (*this)[first_]; }
    typename std::vector<T>::iterator back()
    {
        return (*this)[end_];
    }
    typename std::vector<T>::iterator begin()
    { return std::vector<T>::begin() + first_; }

    typename std::vector<T>::iterator end()
    { return std::vector<T>::size() != 0?std::vector<T>::begin() + end_:novalue(); }

    typename std::vector<T>::iterator before(typename std::vector<T>::iterator where)
    {
        if(where == begin())
            return novalue();
        if(where == std::vector<T>::begin())
            where = std::vector<T>::end() - 1;
        else
            --where;
        return where;
    }
    typename std::vector<T>::iterator after(typename std::vector<T>::iterator where)
    {
        if(where == end() || std::vector<T>::empty())
            return novalue();
        if(where == std::vector<T>::end() - 1)
            where = std::vector<T>::begin();
        else
            ++where;
        return where;
    }
    typename std::vector<T>::iterator novalue()
    {
        return std::vector<T>::end();
    }
};
